import {
  refund
} from '../../config/https'; // 申请售后商品数据

const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    orderid: '',        //订单id
    orderList: '',      //数据列表
    AllSelect: '',      //全选
    selectArr: []       //选中的订单id
  },
  getData() {
    refund({
      id: this.data.orderid
    }).then((res) => {
      let goodsList = res.goods
      goodsList.map(item => {
        item.checkbox = false
      })
      this.setData({
        orderList: goodsList
      })
    })
  },
  //单选选中 || 单选取消
  selectChange(e){
    let index = e.currentTarget.dataset.index
    let Arr = []         //选中的订单id
    let orderList = this.data.orderList
    let radioChk = `orderList[${index}].checkbox`
    orderList[index].checkbox = e.detail
    let chkAll = orderList.every(item => {
      return item.checkbox
    })
    orderList.map(item => {
      if(item.checkbox){
        Arr.push(item.order_goods_id)
      }
    })
    this.setData({
      AllSelect: chkAll,
      [radioChk]: e.detail,
      selectArr: Arr
    })
  },
  //全部选中 || 全部取消
  onAllChange(e){
    let orderList = this.data.orderList
    let Arr = []     //选中的订单id
    orderList.map(item => {
      item.checkbox = e.detail
      if(e.detail){
        Arr.push(item.order_goods_id)
      }
    })
    this.setData({
      orderList,
      AllSelect: e.detail,
      selectArr: Arr
    })
  },
  goRefund(e){
    let orderid = this.data.orderid
    let selectArr = this.data.selectArr
    if(selectArr.length ==  0){
      app.$tip('请选择退款商品')
      return
    }
    wx.navigateTo({
      url: '/pages/Theorderthrfour/Theorderthrfour?id=' + orderid + "&selectArr=" + selectArr
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      orderid: options.id
    })
    this.getData()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})